package core.controls;
import ibm.widgets.WButton;
import ibm.widgets.WCheckBox;

import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class CheckBox extends Control<WCheckBox>
{
	public CheckBox(String propertyValue, String propertyName, String objClass,
			TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		this.obj = null;
	}

	/**
	 * Description :  Select check box
	 * 
	 * @param propValue - property value which is used for check box determination
	 * 
	 * @param propName - property name which is used for check box determination
	 * 
	 * @param parent - area in which the check box belongs.
	 */
	public boolean check(boolean selectAction)
			throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WCheckBox checkBoxToSelect = null;

		// Perform select check box
		try {
			//Allocate Check box
			checkBoxToSelect = this.get();
			
			//Apply needed operation
			if (selectAction) {
				checkBoxToSelect.select();
			} else {
				checkBoxToSelect.deselect();
			}
	
			result = true;
		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on selecting " + this.getDescriptiveName());
		}
		finally
		{
			//Unregister object
			checkBoxToSelect.unregister();
		}

		return result;
	}
	
	/**
	 * Description :  Verify check box state
	 * 
	 * @param expectedState - expected state
	 * 
	 * @param propValue - property value which is used for check box determination
	 * 
	 * @param propName - property name which is used for check box determination
	 * 
	 * @param parent - area in which the check box belongs.
	 */
	public boolean verifyState(boolean expectedState)
			throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WCheckBox checkBoxToSelect = null;

		// Get check box state
		try {
			//Allocate Check box
			checkBoxToSelect = this.get();
			
			//Verify expected state
			if (checkBoxToSelect.isChecked() != expectedState) {
				throw new ResultFailureException(
						"Check box has an unexpected state!");
			}
		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on selecting " + this.getDescriptiveName());
		}
		finally
		{
			//Unregister object
			checkBoxToSelect.unregister();
		}

		return result;
	}

	@Override
	public WCheckBox get() {
		if(this.isCognosControl())
		{
			TestObject parent = this.getParent();
			this.obj = new WCheckBox(parent.getChildren()[0]);
		}
		else
		{
			Regex toSearch = new Regex(this.PropValue(), Regex.MATCH_MULTILINE);
			this.obj = new WCheckBox(toSearch,this.PropName(),this.getObjClass(),this.getParent());
		}
		return this.obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}
}

